import { app as s, BrowserWindow as u, ipcMain as p } from "electron";
import { fileURLToPath as P } from "node:url";
import l from "node:path";
import g from "node:fs";
const f = l.dirname(P(import.meta.url));
process.env.APP_ROOT = l.join(f, "..");
const d = process.env.VITE_DEV_SERVER_URL, C = l.join(process.env.APP_ROOT, "dist-electron"), m = l.join(process.env.APP_ROOT, "dist");
process.env.VITE_PUBLIC = d ? l.join(process.env.APP_ROOT, "public") : m;
let o;
function R() {
  process.defaultApp ? process.argv.length >= 2 && s.setAsDefaultProtocolClient("modelSearch", process.execPath, [l.resolve(process.argv[1])]) : s.setAsDefaultProtocolClient("modelSearch");
}
function _() {
  if (console.log("Setting up protocol launch handlers..."), s.on("open-url", (t, r) => {
    console.log("macOS open-url event:", r), t.preventDefault(), a(r);
  }), s.on("second-instance", (t, r) => {
    console.log("Second instance detected, command line:", r, "_event:", t, "win:", o), o && (o.isMinimized() && o.restore(), o.focus());
    const c = r.find((v) => v.startsWith("modelsearch://"));
    console.log("protocolUrl:", c), c && (console.log("Found protocol URL in second instance:", c), a(c));
  }), !s.requestSingleInstanceLock()) {
    console.log("Another instance is running, quitting..."), s.quit();
    return;
  }
  const e = process.argv.find((t) => t.startsWith("modelSearch://"));
  e && (console.log("Found protocol URL in startup args:", e), a(e));
}
function a(n) {
  console.log("Received protocol URL:", n);
  const t = new URL(n).searchParams.get("file");
  t && w(t);
}
let i = [];
function w(n) {
  try {
    const e = l.resolve(n);
    if (!g.existsSync(e)) {
      console.error("File does not exist:", e);
      return;
    }
    const t = g.readFileSync(e, "utf-8");
    console.log("File content read successfully, length:", t.length);
    const r = {
      filePath: e,
      content: t,
      timestamp: (/* @__PURE__ */ new Date()).toISOString()
    };
    S("file-content-received", r);
  } catch (e) {
    console.error("Error reading temp file:", e);
  }
}
function S(n, e) {
  o && o.webContents.isLoading() ? (console.log("Renderer is still loading, queuing message:", n), i.push({ channel: n, data: e })) : o ? (console.log("Sending message to renderer:", n), o.webContents.send(n, e)) : (console.log("Window not ready, queuing message:", n), i.push({ channel: n, data: e }));
}
function E() {
  if (i.length > 0 && o) {
    console.log("Processing message queue, count:", i.length);
    const n = [...i];
    i = [], n.forEach(({ channel: e, data: t }) => {
      o == null || o.webContents.send(e, t);
    });
  }
}
function L() {
  p.on("file-upload-completed", (n, e) => {
    console.log("File upload completed:", e);
  }), p.on("file-upload-failed", (n, e) => {
    console.error("File upload failed:", e);
  });
}
function h() {
  o = new u({
    icon: l.join(process.env.VITE_PUBLIC, "electron-vite.svg"),
    webPreferences: {
      preload: l.join(f, "preload.mjs"),
      nodeIntegration: !1,
      contextIsolation: !0
    }
  }), o.webContents.on("did-finish-load", () => {
    o == null || o.webContents.send("main-process-message", (/* @__PURE__ */ new Date()).toLocaleString()), E();
  }), d ? o.loadURL(d) : o.loadFile(l.join(m, "index.html"));
}
s.on("window-all-closed", () => {
  process.platform !== "darwin" && (s.quit(), o = null);
});
s.on("activate", () => {
  u.getAllWindows().length === 0 && h();
});
s.whenReady().then(() => {
  R(), L(), _(), h();
});
export {
  C as MAIN_DIST,
  m as RENDERER_DIST,
  d as VITE_DEV_SERVER_URL
};
